package com.example.controller;

import com.example.pojo.Country;
import com.example.pojo.CountryExample;
import com.example.pojo.EnemyAttribute;
import com.example.service.CountryService;
import com.example.service.EnemyAttributeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.IncorrectResultSetColumnCountException;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.jws.WebParam;
import javax.validation.Valid;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author ftl douyacai
 * @create 2017/12/1
 */
@RequestMapping(value = "/test")
@RestController
public class CountryController {

    @Autowired
    private CountryService countryService;

    @Autowired
    private EnemyAttributeService enemyAttributeService;

    @GetMapping(value = "/list")
    public ModelAndView list(Map<String, Object> map) {
        List<EnemyAttribute> attributeList = new ArrayList<>();
        List<Country> countryList = new ArrayList<>();
        try {
            countryList = countryService.findAll_enemy(null);
            attributeList = enemyAttributeService.findAll(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        map.put("countryList", countryList);
        map.put("attributeList", attributeList);
        return new ModelAndView("/demo/list");
    }

    @GetMapping(value = "/detail")
    public ModelAndView detail(@RequestParam("id") Integer id,
                               Map<String, Object> map) {
        Country country = new Country();
        try {
            country = countryService.findOne(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        map.put("country", country);
        return new ModelAndView("/demo/detail", map);
    }

    @RequestMapping("saveList")
    public ModelAndView saveList(Map<String, Object> map) {
        List<EnemyAttribute> attributeList = enemyAttributeService.findAll(null);
        map.put("attributeList", attributeList);
        return new ModelAndView("demo/save", map);
    }

    @PostMapping("/save")
    public ModelAndView save(@Valid Country country,
                             BindingResult result,
                             Map<String, Object> map) {
        // 判断提交过来的表单是否有错误
        if (result.hasErrors()) {
            map.put("msg", result.getFieldError().getDefaultMessage());
            return new ModelAndView("common/error", map);
        }
        Country resultCountry = new Country();
        try {
            resultCountry.setEnemyAttributeId(country.getEnemyAttributeId());
            resultCountry.setCountryName(country.getCountryName());
            resultCountry.setEnglishAbb(country.getEnglishAbb());
            resultCountry.setImgName(country.getImgName());
            resultCountry.setImgPath(country.getImgPath());
            resultCountry.setCreateTime(new Date());
            countryService.insert(resultCountry);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("msg", e.getMessage());
            map.put("url", "sell/test/saveList");
            return new ModelAndView("common/error", map);
        }
        map.put("url", "/sell/test/list");
        return new ModelAndView("common/success", map);
    }

    @GetMapping(value = "/update")
    public ModelAndView update(@RequestParam("id") Integer id,
                               Map<String, Object> map) {
        Country country = new Country();
        List<EnemyAttribute> attributeList = new ArrayList<>();
        try {

            country = countryService.findOne(id);
            attributeList = enemyAttributeService.findAll(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        map.put("country", country);
        map.put("attributeList", attributeList);
        return new ModelAndView("/demo/update", map);
    }

    @PostMapping(value = "/update_save")
    public ModelAndView update_save(@Valid Country country,
                                    BindingResult result,
                                    Map<String, Object> map) {
        if (result.hasErrors()) {
            map.put("msg", result.getFieldError().getDefaultMessage());
            return new ModelAndView("common/error", map);
        }
        Country resultCountry = new Country();
        try {
            if (!StringUtils.isEmpty(country.getId())) {
//                resultCountry = countryService.findOne(country.getId());
                resultCountry = countryService.selectByPrimaryKey(country.getId());
            }
            resultCountry.setCountryName(country.getCountryName());
            resultCountry.setEnglishAbb(country.getEnglishAbb());
            resultCountry.setEnemyAttributeId(country.getEnemyAttributeId());
            resultCountry.setImgName(country.getImgName());
            resultCountry.setImgPath(country.getImgPath());
            resultCountry.setCreateTime(new Date());
//            countryService.insert(resultCountry);
            countryService.updateByPrimaryKey(resultCountry);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("msg", e.getMessage());
            map.put("url", "sell/test/update");
            return new ModelAndView("common/error", map);
        }
        map.put("url", "/sell/test/list");
        return new ModelAndView("common/success", map);
    }

    @GetMapping(value = "/delete")
    public ModelAndView delete(@RequestParam("id") Integer id,
                               Map<String, Object> map) {
        try {
            Country country = countryService.findOne(id);
            countryService.deleteByPrimaryKey(id);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("msg", e.getMessage());
            map.put("url", "sell/test/list");
            return new ModelAndView("common/error", map);
        }
        map.put("url", "/sell/test/list");
        return new ModelAndView("common/success", map);
    }

    @GetMapping(value = "/query")
    public ModelAndView query(@Valid Country country,
                              BindingResult result,
                              Map<String, Object> map) {
        if (result.hasErrors()) {
            map.put("msg", result.getFieldError().getDefaultMessage());
            return new ModelAndView("common/error", map);
        }
        List<Country> countryList = new ArrayList<>();
        try {
            countryList = countryService.WithEnemyAttributeSelectLike(country.getCountryName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        map.put("countryList", countryList);
        return new ModelAndView("/demo/list");
    }

    @GetMapping(value = "/query1")
    public ModelAndView query1(@Valid Country country,
                               BindingResult result,
                               Map<String, Object> map) {
        if (result.hasErrors()) {
            map.put("msg", result.getFieldError().getDefaultMessage());
            return new ModelAndView("common/error", map);
        }
        List<Country> countryList = new ArrayList<>();
        List<EnemyAttribute> attributeList = new ArrayList<>();
        try {
            countryList = countryService.WithEnemyAttributeSelectLike1(country.getCountryName(), country.getEnemyAttributeId());
            attributeList = enemyAttributeService.findAll(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        map.put("countryList", countryList);
        map.put("attributeList", attributeList);
        return new ModelAndView("/demo/list");
    }



}
